<?php

namespace app\mobileapp\controller;

use \think\Db;

class Pay extends Base
{

    /**
     * User: Simony
     * Function:购买套餐
     */
    public function buyPackage()
    {
        $memberId = $this->memberId;
        $pay_type = input('pay_type/d', 0);
        $package_id = input('package_id/d', 0);


        Db::startTrans();
        //无邀请码绑定关系
        $memberModel = model('common/member/Member');
        $member_invite_code = $memberModel->getVal('invite_code', ['id' => $memberId]);
        if (empty($member_invite_code)) { //不存在邀请码

            $company_invite_code = $memberModel->getVal('invite_code', ['is_company' => 1]);
            if (empty($company_invite_code)) {
                Db::rollback();
                $this->api_ajaxReturn(ERROR, '不存在公司邀请码');
            }
            $res = model('common/member/MemberLogic', 'logic')->setInviteCode($memberId, $company_invite_code);
            if ($res['code'] != SUCCESS) {
                Db::rollback();
                $this->api_ajaxReturn($res['code'], $res['msg'], $res['result']);
            }
        }


        //购买套餐
        $res = model('common/package/PackageLogic', 'logic')->buyPackage($memberId, $package_id, $pay_type);
        if ($res['code'] != SUCCESS) {
            Db::rollback();
            $this->api_ajaxReturn($res['code'], $res['msg'], $res['result']);
        }


        $bonusLogic = model('common/BonusLogic', 'logic');
        $commission_res = $bonusLogic->commissionBonus($memberId, $package_id);
        if ($commission_res['code'] != SUCCESS) {
            Db::rollback();
            $this->api_ajaxReturn(ERROR, '佣金结算异常');
        }
        $partner_res = $bonusLogic->partnerBonus($memberId, $package_id);
        if ($partner_res['code'] != SUCCESS) {
            Db::rollback();
            $this->api_ajaxReturn(ERROR, '合伙人奖结算异常');
        }
        $grant_bonus_res = $bonusLogic->grantBonus(model('common/bonus/Bonus')::SECOND_GRANT); //发放奖金


        //购买赠送
        $packageLogic= model('common/package/PackageLogic', 'logic');
        $give_res = $packageLogic->givePackage($memberId, $packageLogic::PAY_GIVE);
        if ($give_res['code'] != SUCCESS) {
            Db::rollback();
            $this->api_ajaxReturn($give_res['code'], $give_res['msg']);
        }

        Db::commit();

        $noticleLogic = model('common/Noticle', 'logic');
        $noticleLogic->sendAutoTxt($memberId, $noticleLogic::VIP_NOTICE); //每次低等级升级vip都需要调用
        $this->api_ajaxReturn($res['code'], $res['msg'], $res['result']);
    }


    /**
     * User: Simony
     * Function:付费解锁 （相册or照片）
     */
    public function payUnlock()
    {
        $memberId = $this->memberId;
        $file_id = input('file_id/d', 0);
        $type = input('type/d', 1);
        if ($file_id <= 0) $this->api_ajaxReturn(ERROR, '参数错误');
        if (!in_array($type, [1, 2])) $this->api_ajaxReturn(ERROR, '解锁类型错误');

        $authLogic = model('common/AuthLogic', 'logic');
        $use_auth_res = $authLogic->isUseAuth($memberId, true); //校验使用权限
        if (isset($use_auth_res['code']) && $use_auth_res['code'] != SUCCESS) $this->api_ajaxReturn($use_auth_res['code'], $use_auth_res['msg'], $use_auth_res['result']);


        Db::startTrans();
        $res = model('common/photo/PhotoPayLogic', 'logic')->payUnlock($memberId, $file_id, $type);
        $res['code'] == SUCCESS ? Db::commit() : Db::rollback();

        $this->api_ajaxReturn($res['code'], $res['msg'], $res['result']);
    }

    /**
     * User: Simony
     * Function:付费解锁（社交：微信号+聊天)
     */
    public function payViewSocialAccount()
    {
        $memberId = $this->memberId;
        $show_member_id = input('show_member_id/d', 0);

        Db::startTrans();
        $res = model('common/member/MemberLogic', 'logic')->payViewSocialAccount($memberId, $show_member_id);
        $res['code'] == SUCCESS ? Db::commit() : Db::rollback();

        $this->api_ajaxReturn($res['code'], $res['msg'], $res['result']);
    }

    /**
     * User: Simony
     * Function:充值选项列表
     */
    public function rechargeOptions()
    {
        $recharge_options_list = model('common/member/RechargeOptions')->getList(['state' => 1], 6, 'id,money,price', 'sort desc');

        $data=[
            'recharge_options_list'=>$recharge_options_list,
        ];


        $this->api_ajaxReturn(SUCCESS, '获取成功', $data);
    }


}
